import login from '@/utils/login'
import Cookies from "js-cookie";
import {
 getToken, setToken, removeToken,
 setUserName,getUserName,removeUserName,
 setAvatar,getAvatar,removeAvatar,
 setUserId,getUserId,removeUserId,
 setEmail,getEmail,removeEmail,
 setIntroduce,getIntroduce,removeIntroduce,
 setSex,getSex,removeSex,
 setPhoneNumber,getPhoneNumber,removePhoneNumber,
 setRegTime,getRegTime,removeRegTime,
 setLastUpdateTime,getLastUpdateTime,removeLastUpdateTime
} from '@/utils/auth'


const user={
    state: {
        token:getToken(),
        username:getUserName(),
        avatar:getAvatar(),
        roles:[],
        userId:getUserId(),
        email:getEmail(),
        phoneNumber:getPhoneNumber(),
        sex:getSex(),
        introduce:getIntroduce(),
        regTime:getRegTime(),
        lastUpdateTime:getLastUpdateTime(),
        backgroundImg:''
    },

    mutations: {
        SET_TOKEN:(state,token)=>{
            state.token=token
        },
        SET_NAME:(state,username)=>{
            state.username=username
        },
        SET_AVATAR:(state,avatar)=>{
            state.avatar=avatar
        },
        SET_ROLES:(state,roles)=>{
            state.roles=roles
        },
        SET_USERID:(state,userId)=>{
            state.userId=userId
        },
        SET_EMAIL:(state,email)=>{
            state.email=email
        },
        SET_INTRODUCE:(state,introduce)=>{
            state.introduce=introduce
        },
        SET_SEX:(state,sex)=>{
            state.sex=sex
        },
        SET_PHONENUMVER:(state,phoneNumber)=>{
            state.phoneNumber=phoneNumber
        },
        SET_REGTIME:(state,regTime)=>{
            state.regTime=regTime
        },
        SET_LASTUPDATETIME:(state,lastUpdateTime)=>{
            state.lastUpdateTime=lastUpdateTime
        },
        SET_BACKGROUNDIMG:(state,backgroundImg)=>{
            state.backgroundImg=backgroundImg
        }

    },
    actions:{
        Login({commit},userInfo){
            const username=userInfo.username.trim()
            return new Promise((resolve,reject)=>{
            login.getLogin(username,userInfo.password).then(res=>{
                setToken(res.token)
                commit('SET_TOKEN',res.token)
                resolve()
            }).catch(error=>{
                reject(error)
            })
        })
        },
        GetInfo({commit}){
            return new Promise((resolve,reject)=>{
                 login.getUserInfo().then(res=>{
                    const data=res.data
                        commit('SET_NAME',data.username)
                        setUserName(data.username)
                        commit('SET_AVATAR',data.avatar)
                        setAvatar(data.avatar)
                        commit('SET_USERID',data.userId)
                        setUserId(data.userId)
                        commit('SET_EMAIL',data.email)
                        setEmail(data.email)
                        commit('SET_INTRODUCE',data.introduce)
                        setIntroduce(data.introduce)
                        commit('SET_SEX',data.sex==1?'男':'女')
                        setSex(data.sex==1?'男':'女')
                        commit('SET_PHONENUMVER',data.phoneNumber)
                        setPhoneNumber(data.phoneNumber)
                        commit('SET_REGTIME',data.regTime)
                        setRegTime(data.regTime)
                        commit('SET_LASTUPDATETIME',data.lastUpdateTime)
                        setLastUpdateTime(data.lastUpdateTime)

                 }).catch(error=>{
                    reject(error)
                 })
                })
        },
        Logout({commit}){
            return new Promise((resolve,reject)=>{
                login.getLoginOut().then(res=>{
                    commit('SET_TOKEN','')
                    removeToken()
                    removeUserName()
                    removeAvatar()
                    removeUserId()
                    removeEmail()
                    removeIntroduce()
                    removeSex()
                    removePhoneNumber()
                    removeRegTime()
                    removeLastUpdateTime()
                    resolve()
                }).catch(error=>{
                    reject(error)
                })
            })
        },
        uploadBackgroundImg({commit},backgroundImg){
            commit('SET_BACKGROUNDIMG',backgroundImg)
        }
    }
}
export default user
